import type { AddReq } from '@fast-crud/fast-crud';

import type { AxiosRequestConfig } from '@vben/request';
import type { UserInfo } from '@vben/types';

import type { UserModel } from './model/user';

import { requestClient } from '#/api/request';
import { ServicePrefixEnum } from '#/enums/commonEnum';

export const UserConfig = {
  UpdateBaseInfo: {
    url: `${ServicePrefixEnum.OAUTH}/anyone/baseInfo`,
    method: 'PUT',
  } as AxiosRequestConfig,

  UpdateAvatar: {
    url: `${ServicePrefixEnum.OAUTH}/anyone/avatar`,
    method: 'PUT',
  } as AxiosRequestConfig,
  UpdatePassword: {
    url: `${ServicePrefixEnum.OAUTH}/anyone/password`,
    method: 'PUT',
  } as AxiosRequestConfig,
  UpdateEmail: {
    url: `${ServicePrefixEnum.OAUTH}/anyone/email`,
    method: 'PUT',
  } as AxiosRequestConfig,
  UpdateMobile: {
    url: `${ServicePrefixEnum.OAUTH}/anyone/mobile`,
    method: 'PUT',
  } as AxiosRequestConfig,
};
/**
 * 获取用户信息
 */
export async function getUserInfoApi() {
  return requestClient.get<UserInfo>(
    `${ServicePrefixEnum.OAUTH}/anyone/getUserInfoById`,
  );
}

/** 检测员工是否拥有指定应用的权限 */
export async function checkEmployeeHaveApplication(applicationId: string) {
  return requestClient.get<boolean>(
    `${ServicePrefixEnum.OAUTH}/anyone/checkEmployeeHaveApplication`,
    { params: { applicationId } },
  );
}

/**
 * 扮演用户
 * @param data 参数
 */
export async function playUser(data: any) {
  return requestClient.post(`${ServicePrefixEnum.OAUTH}/sys/playUser`, data);
}

/**
 * 退出扮演
 * @param data 参数
 */
export async function unPlayUser(data: any) {
  return requestClient.post(`${ServicePrefixEnum.OAUTH}/sys/unPlayUser`, data);
}

export namespace UserApi {
  /* 修改个人资料 */
  export async function updateBaseInfo(
    data: UserModel.DefUserBaseInfoUpdateVO,
  ) {
    return requestClient.put(UserConfig.UpdateBaseInfo.url as string, data);
  }
  export async function updatePassword(
    data: UserModel.DefUserPasswordUpdateVO,
  ) {
    return requestClient.put(UserConfig.UpdatePassword.url as string, data);
  }

  export async function updateEmail(data: UserModel.DefUserEmailUpdateVO) {
    return requestClient.put(UserConfig.UpdateEmail.url as string, data);
  }

  export async function updateMobile(data: UserModel.DefUserMobileUpdateVO) {
    return requestClient.put(UserConfig.UpdateMobile.url as string, data);
  }

  export const updateBaseInfoRequest = async (req: AddReq) => {
    const { form } = req;
    return await updateBaseInfo(form);
  };

  export const updatePasswordRequest = async (req: AddReq) => {
    const { form } = req;
    return await updatePassword(form);
  };

  export const updateMobileRequest = async (req: AddReq) => {
    const { form } = req;
    return await updateMobile(form);
  };
  export const updateEmailRequest = async (req: AddReq) => {
    const { form } = req;
    return await updateEmail(form);
  };
}
